package com.health.db.model;

import java.util.List;


public class PageBean {

    public static int DEFAULT_SIZE = 20;
    private List list;        

    private int allRow;        
    private int totalPage;       
    private int currentPage;   
    private int pageSize;        
    private int offset;

    private boolean isFirstPage;    
    private boolean isLastPage;     
    private boolean hasPreviousPage;   
    private boolean hasNextPage;       


    private int prePage;
    private int nextPage;

    private int size;

    public int getSize() {
        return pageSize + 1;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getNextPage() {
        if (getHasNextPage()) {
            nextPage = currentPage + 1;
        } else {
            nextPage = totalPage;
        }
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getPrePage() {
        if (getHasPreviousPage()) {
            prePage = currentPage - 1;
        } else {
            prePage = 1;
        }
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public PageBean() {

    }

    public PageBean(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getAllRow() {
        return allRow;
    }

    public void setAllRow(int allRow) {
        this.allRow = allRow;
        this.pageSize = this.pageSize > 0 ? this.pageSize : DEFAULT_SIZE;
        if (this.currentPage == 0) {
            this.currentPage = 1;
        }
      
        this.totalPage = countTotalPage(this.pageSize, allRow);
        this.offset = countOffset(this.pageSize, this.getCurrentPage());
        
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public void init() {
        this.isFirstPage = isFirstPage();
        this.isLastPage = isLastPage();
    }

    public boolean isFirstPage() {
        return currentPage == 1;    // ���ǵ�ǰҳ�ǵ�1ҳ
    }

    public boolean isLastPage() {
        return currentPage == totalPage;    //���ǰҳ�����һҳ
    }


    public boolean getHasNextPage() {
        return hasNextPage = currentPage != totalPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean getHasPreviousPage() {
        return hasPreviousPage = currentPage != 1;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public static int countTotalPage(final int pageSize, final int allRow) {
        int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1;
        return totalPage;
    }

    public static int countOffset(final int pageSize, final int currentPage) {
        final int offset = pageSize * (currentPage - 1);
        return offset;
    }


    public static int countCurrentPage(int page) {
        final int curPage = (page == 0 ? 1 : page);
        return curPage;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

}